package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.mapper.MemberMapper;
import com.mapper.OrderMapper;
import com.mapper.SetmealMapper;
import com.pojo.MemberExample;
import com.pojo.Order;
import com.pojo.OrderExample;
import com.service.ReportService;
import com.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;



    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        /**
         *  reportDate  日期
         *  todayNewMember: 0,      今日新增会员数
         *  totalMember: 0,         总会员数
         *  thisWeekNewMember: 0,   本周新增会员数
         *  thisMonthNewMember: 0,  本月新增会员数
         *  todayOrderNumber: 0,    今日预约数
         *  todayVisitsNumber: 0,   今日到诊数
         *  thisWeekOrderNumber: 0, 本周预约数
         *  thisWeekVisitsNumber: 0,本周到诊数
         *  thisMonthOrderNumber: 0,本月预约数
         *  thisMonthVisitsNumber: 0,本月到诊数
         *  hotSetmeal: [            热门套餐
         *    {name: '阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐', setmeal_count: 200, proportion: 0.222},
         *     {name: '阳光爸妈升级肿瘤12项筛查体检套餐', setmeal_count: 200, proportion: 0.222}
         *   ]
         * */

        Map<String,Object> map=new HashMap<>();
        //今日日期
        Date today = DateUtils.getToday();
        String reportDate = DateUtils.parseDate2String(today);
        map.put("reportDate",reportDate);


        //今日新增会员数
        MemberExample me=new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andRegtimeEqualTo(today);
        long todayNewMember = memberMapper.countByExample(me);
        map.put("todayNewMember",todayNewMember);

        //总会员数
        long totalMember = memberMapper.countByExample(null);
        map.put("totalMember",totalMember);

        //本周新增会员数
        MemberExample me2=new MemberExample();
        MemberExample.Criteria mec2 = me2.createCriteria();
        mec2.andRegtimeGreaterThanOrEqualTo(DateUtils.getThisWeekMonday());
        long thisWeekNewMember = memberMapper.countByExample(me2);
        map.put("thisWeekNewMember",thisWeekNewMember);

        //本月新增会员数
        MemberExample me3=new MemberExample();
        MemberExample.Criteria mec3 = me3.createCriteria();
        mec3.andRegtimeGreaterThanOrEqualTo(DateUtils.getFirstDay4ThisMonth());
        long thisMonthNewMember = memberMapper.countByExample(me3);
        map.put("thisMonthNewMember",thisMonthNewMember);

        //今日预约数
        OrderExample oe=new OrderExample();
        OrderExample.Criteria oec = oe.createCriteria();
        oec.andOrderdateEqualTo(today);
        List<Order> orderList = orderMapper.selectByExample(oe);
        int todayOrderNumber = orderList.size();
        map.put("todayOrderNumber",todayOrderNumber);

        //今日到诊数
        int todayVisitsNumber=0;
        for (Order order : orderList) {
            if("已到诊".equals(order.getOrderstatus())){
                todayVisitsNumber++;
            }
        }
        map.put("todayVisitsNumber",todayVisitsNumber);

        //本周预约数
        OrderExample oe2=new OrderExample();
        OrderExample.Criteria oec2 = oe2.createCriteria();
        oec2.andOrderdateGreaterThanOrEqualTo(DateUtils.getThisWeekMonday());
        List<Order> orderList2 = orderMapper.selectByExample(oe2);
        int thisWeekOrderNumber = orderList2.size();
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);

        //本周到诊数
        int thisWeekVisitsNumber=0;
        for (Order order : orderList2) {
            if("已到诊".equals(order.getOrderstatus())){
                thisWeekVisitsNumber++;
            }
        }
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

        //本月预约数
        OrderExample oe3=new OrderExample();
        OrderExample.Criteria oec3 = oe3.createCriteria();
        oec3.andOrderdateGreaterThanOrEqualTo(DateUtils.getFirstDay4ThisMonth());
        List<Order> orderList3 = orderMapper.selectByExample(oe3);
        int thisMonthOrderNumber = orderList3.size();
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);

        //本月到诊数
        int thisMonthVisitsNumber=0;
        for (Order order : orderList3) {
            if("已到诊".equals(order.getOrderstatus())){
                thisMonthVisitsNumber++;
            }
        }
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        //热门套餐
        List<Map<String, Object>> hotSetmeal = setmealMapper.getHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);


        return map;
    }
}
